package com.c4u.sds.controler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.*;
import java.nio.charset.Charset;

/**
 * Created by Peter on 2018/3/21.
 */
@Controller
@RequestMapping("/")
public class IndexController {
    private static Logger logger = LoggerFactory.getLogger(IndexController.class);

    public String index(){
        return "index";
    }

    @RequestMapping("exec/{cmd}")
    public String exec(@PathVariable("cmd") String cmd){
        logger.info("exec command:"+cmd);
        try {
            Process process = Runtime.getRuntime().exec(cmd);
            InputStream in = process.getInputStream();
            printOs(in);
        } catch (IOException e) {
            logger.error(e.getMessage(),e);
        }
        return "index";
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        Process process = Runtime.getRuntime().exec("cmd.exe dir d:");
        String s;
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(),"GB2312"));
        //获取系统默认的字符编码
        while((s=bufferedReader.readLine()) != null) {
            System.out.println(s);
        }
        process.waitFor();
    }
    private static void printOs(InputStream in) throws IOException {
        int ai = in.available();
        int i=0;
        while((i = in.read())>0){
            System.out.print(i);
        }
    }
}
